Vue.component("upload_file_comp",{
	props:['account'],
	data:function(){
		return {
			buzy:false,
			url:null,
			action:null,
			fileobj:null,			
			formData:null,
			file_type:null,
			remember:null,
			max_size:null,
			accept:null,
			data:{},
			
		};
	},
	ready:function(){
		
	},
	template:loadHTML("components/upload_file_comp/upload_file_comp.html"),
	methods:{
		change:function(){
			if(this.action=="headicon"){
				
				var file=$("#file_").prop("files");
				if(file.length==0){
					return;
				}
				console.log("当前图片大小："+file[0].size+" 限制大小："+3*1024*1024);
				if(file[0].size>=this.max_size){
					console.log("图片过大");
					return;
				}
				this.fileobj=file[0];
				this.formData = new FormData();
				
				
				this.formData.append('file', $('#file_')[0].files[0]);
				this.formData.append("account",this.account);
				
				this.upload();
			}
			if(this.action=="send_file"){
				this.accept="";
				var file=$("#file_").prop("files");
				console.log(file.length);
				if(file.length==0){
					return;
				}
				console.log("当前文件大小："+file[0].size+" 限制大小："+1024*1024*1024);
				if(file[0].size>=this.max_size){
					console.log("文件过大");
					return;
				}
				this.fileobj=file[0];
				this.formData = new FormData();
				
				
				this.formData.append('file', $('#file_')[0].files[0]);
				this.formData.append("account",this.account);
				this.formData.append("chat_type",this.data.chat_type);
				this.formData.append("friAccount",this.data.friAccount);
				
				this.upload();
				
			
			}
			
		},
		init:function(data){
			if(this.buzy==true){
				return;
			}
			this.data=data;
			if(data.action=="headicon"){
				//修改头像
				this.accept="image/*";
				this.file_type="image/*";
				this.max_size=3*1024*1024;
				this.url="headicon";
				this.action=data.action;
				console.log($("#file_upload"));
				
				
				
			}
			if(data.action=="send_file"){
				this.accept="";
				this.file_type=null;
				this.url="send_file";
				this.max_size=1024*1024*1024;
				this.action=data.action;
				
			
			}
			$("#file_").click();
			
		},
		click:function(){
			
		},
		upload:function(){
			
			$.ajax({
			    url: this.url,
			    type: 'POST',
			    cache: false,
			    data: this.formData,
			    processData: false,
			    contentType: false,
			    success:function(data){
			    	console.log(data);
					if(data.success){
						console.log("上传成功");	
						if(this.action=="headicon"){
							//通知父组件更新用户头像
							this.$emit("update_icon",data.data);
						}
						if(this.action=="send_file"){
							console.log("文件上传成功了");
						}
					}
					
					this.formData=null;
					
					this.buzy=false;
					
			    }.bind(this)
			}).fail(function(res) {
				console.log("上传失败");
				this.formData=null;
				this.buzy=false;
			}.bind(this));
		}
	},
});